package com.example.negativescreendemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.negativescreendemo.adapter.NegativeScreenAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author: njb
 * @date: 2025/8/20 21:18
 * @desc: 描述
 */
public class NegativeScreenActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private NegativeScreenAdapter adapter;
    private List<CardItem> cardItems;
    private TextView timeTextView;
    private TextView dateTextView;
    private TextView weatherTextView;
    private ImageView weatherIcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_negative_screen);

        // 初始化视图
        initViews();

        // 初始化数据
        initData();

        // 设置适配器
        adapter = new NegativeScreenAdapter(this, cardItems);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        // 设置点击事件
        adapter.setOnItemClickListener(position -> {
            CardItem item = cardItems.get(position);
            Toast.makeText(NegativeScreenActivity.this,
                    "点击了: " + item.getTitle(), Toast.LENGTH_SHORT).show();
        });

        // 更新时间
        updateTime();
    }

    private void initViews() {
        recyclerView = findViewById(R.id.recycler_view);
        timeTextView = findViewById(R.id.time_text);
        dateTextView = findViewById(R.id.date_text);
        weatherTextView = findViewById(R.id.weather_text);
        weatherIcon = findViewById(R.id.weather_icon);
    }

    private void initData() {
        // 模拟天气数据
        weatherTextView.setText("25°C 晴朗");

        // 初始化卡片数据
        cardItems = new ArrayList<>();

        // 添加快捷功能卡片
        List<ActionItem> actionItems = new ArrayList<>();
        actionItems.add(new ActionItem("扫一扫", R.drawable.ic_scan));
        actionItems.add(new ActionItem("付款", R.drawable.ic_pay));
        actionItems.add(new ActionItem("乘车码", R.drawable.ic_bus));
        actionItems.add(new ActionItem("健康码", R.drawable.ic_health));
        actionItems.add(new ActionItem("充电宝", R.drawable.ic_power_bank));
        actionItems.add(new ActionItem("更多", R.drawable.ic_more));
        cardItems.add(CardItem.createActionCard(NegativeScreenAdapter.CardType.ACTIONS, "快捷功能", actionItems));

        // 添加日程卡片
        List<ScheduleItem> scheduleItems = new ArrayList<>();
        scheduleItems.add(new ScheduleItem("9:30", "团队周会", "会议室A"));
        scheduleItems.add(new ScheduleItem("14:00", "客户拜访", "XX公司"));
        cardItems.add(CardItem.createScheduleCard(NegativeScreenAdapter.CardType.SCHEDULE, "今日日程", scheduleItems));

        // 添加新闻卡片
        List<NewsItem> newsItems = new ArrayList<>();
        newsItems.add(new NewsItem("最新科技突破：人工智能新进展", "科技日报", "10分钟前"));
        newsItems.add(new NewsItem("本地气温将持续升高，最高达35°C", "本地新闻", "30分钟前"));
        cardItems.add(CardItem.createNewsCard(NegativeScreenAdapter.CardType.NEWS, "热点新闻", newsItems));

        // 添加待办事项卡片
        List<TodoItem> todoItems = new ArrayList<>();
        todoItems.add(new TodoItem("完成项目报告", false));
        todoItems.add(new TodoItem("购买生日礼物", true));
        todoItems.add(new TodoItem("回复客户邮件", false));
        cardItems.add(CardItem.createTodoCard(NegativeScreenAdapter.CardType.TODO, "待办事项", todoItems));
    }

    private void updateTime() {
        // 更新时间和日期
        final Handler handler = new Handler(Looper.getMainLooper());
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                handler.post(() -> {
                    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
                    SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE, MMMM d日", Locale.getDefault());
                    String time = timeFormat.format(new Date());
                    String date = dateFormat.format(new Date());

                    timeTextView.setText(time);
                    dateTextView.setText(date);
                });
            }
        }, 0, 60000); // 每分钟更新一次
    }
}
